package com.leo.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @ClassName NettyClient02
 * @Description 客户端增加连接监听，判断是否连接成功
 * @Author wangss
 * @date 2022.03.21 19:14
 * @Version 1.0
 */
public class NettyClient02 {

    public static void main(String[] args) {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();
        bootstrap
                // 指定线程模型
                .group(workerGroup)
                // 指定IO模型
                .channel(NioSocketChannel.class)
                // IO 处理逻辑
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {

                    }
                });

        // 建立连接
        bootstrap.connect("localhost", 8000).addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("连接成功！！！");
            } else {
                System.out.println("连接失败！！！");
            }
        });
    }
}
